package com.yan.deezer.utils;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;

public class SingletonMediaPlayer {

	// Create Handler to call View updates on the main UI thread.

	static protected MediaPlayer mp = new MediaPlayer();

	static protected Context c;
	static protected String url;

	private static SingletonMediaPlayer sInstance;

	public static SingletonMediaPlayer getInstance() {
		if (sInstance == null) {
			sInstance = new SingletonMediaPlayer();
		}
		return sInstance;
	}

	private SingletonMediaPlayer() {

		mp = new MediaPlayer();

	}

	public void startStreaming(Context mContext, String mUrl)
			throws IOException {

		c = mContext;
		url = mUrl;

		Runnable r = new Runnable() {
			@Override
			public void run() {
				if (mp != null) {
					mp.stop();
					mp.reset();
					mp = new MediaPlayer();

				}
				try {
					mp.setDataSource(c, Uri.parse(url));
					mp.setLooping(false);
					mp.prepare();
					mp.start();
				} catch (Exception e) {
					e.printStackTrace();
				}

				return;

			}
		};
		new Thread(r).start();
	}

}
